<?php
namespace Behavior;
use think\Session;
use app\login\model\Admin as AdminModel;
use app\login\validate\Admin as AdminValidate;
use Service\JsonService;
class LoginBehavior{
    /**
     * 登录检测
     */
    static public function loginCheck($info)
    {
        $validate = new AdminValidate();
        $res = $validate->scene('login')->check($info);
        if($res == false){
            return JsonService::errorResponse($validate->getError());
        }
        $model = AdminModel::adminWithAccount($info['username']);
        if(empty($model)){
            return JsonService::errorResponse("该用户不存在");
        }
        if($model['status']!=0){
            return JsonService::errorResponse('账户已禁用');
            exit();
        }
        $pwd = md5($info['password'].$model['salt']);
        if($model['password']!=$pwd){
            return JsonService::errorResponse('密码输入错误');
        }
        $model->logintime = $info['logintime'];
        $model->loginip = $info['loginip'];
        $res = $model->save();
        $session = new Session();
        $session->set('userId',$model->id);
        $session->set('userInfo',$model);
        return JsonService::successResponse("登录成功");
    }
    static public function isLogin()
     {
        $session = new Session();
        if (($session->get('userId')==null)&($session->get('userInfo')==null)) {
            return false;
        }else{
            return true;
        }
     }
    
}